package com.aisile.common.web.admin;

import com.aisile.model.admin.pojos.AdUser;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.utils.common.AppJwtUtil;
import com.aisile.utils.threadlocal.AdminThreadLocalUtils;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther:yry
 * @Date:2024/4/26 0026
 * @VERSON:1.0
 */
@WebFilter(filterName = "adminTokenFilter",urlPatterns = "/*")
public class AdminTokenFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        ResponseResult responseResult = null;
        try {
            // 先放行登录
            if(request.getRequestURI().contains("/login/in")){
                filterChain.doFilter(request,response);
            }else{
                // 获取token
                String token =  request.getHeader("token");
                if(token == null || "".equals(token.trim())){
                    responseResult = ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
                }else{
                    // 解析token
                    Claims claims = AppJwtUtil.getClaimsBody(token);
                    int i =  AppJwtUtil.verifyToken(claims);
                    if(i>0 || i<-1){
                        // 有问题 或者过期了
                        responseResult = ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
                    }else{
                        // 封装用户信息
                        Integer id = (Integer) claims.get("id");
                        AdUser adUser = new AdUser();
                        adUser.setId(id);
                        AdminThreadLocalUtils.setUser(adUser);
                        if (i == 0) {
                            // 对token续签 refresh_token
                            response.setHeader("refresh_token",AppJwtUtil.getToken(id.longValue()));
                        }
                        // 放行
                        filterChain.doFilter(request,response);
                    }
                }
            }
        }catch (Exception e){
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }finally {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            if(responseResult!=null){
                response.getOutputStream().write(JSON.toJSONString(responseResult).getBytes());
            }
        }
    }
}
